using System;
using ClimbTracker.BusinessLogic.Entities;
using NHibernate;

namespace ClimbTracker.BusinessLogic.Dao
{
    /// <summary>
    /// Summary description for CompetitionDao.
    /// </summary>
    public class CompetitionDao : BaseDao
    {
        public CompetitionDao()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        /// <summary>
        /// Retrieve a Competition object from the database by its ID
        /// </summary>
        /// <param name="competitionID"></param>
        /// <returns></returns>
        public Competition retrieveCompetitionByID(int competitionID)
        {
            Competition competition = null;
            try
            {
                competition = (Competition)currentSession.Get(typeof(Competition), competitionID);
            }
            catch (HibernateException ex)
            {
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }

            return competition;

        }

        /// <summary>
        /// Create a new compeition entry in the database from the given Competition object
        /// </summary>
        /// <param name="competition"></param>
        public void createCompetition(Competition competition)
        {
            ITransaction transaction = null;
            try
            {
                transaction = currentSession.BeginTransaction();

                currentSession.Save(competition);

                transaction.Commit();
            }
            catch (HibernateException ex)
            {
                if (transaction != null)
                    transaction.Rollback();
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }
        }

        /// <summary>
        /// Delete a the database entry of the given Competition object
        /// </summary>
        /// <param name="competition"></param>
        public void deleteCompetition(Competition competition)
        {
            ITransaction transaction = null;
            try
            {
                transaction = currentSession.BeginTransaction();

                currentSession.Delete(competition);

                transaction.Commit();
            }
            catch (HibernateException ex)
            {
                if (transaction != null)
                    transaction.Rollback();
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }
        }

        /// <summary>
        /// Update the database entry of the given Competition object
        /// </summary>
        /// <param name="competition"></param>
        public void updateCompetition(Competition competition)
        {
            ITransaction transaction = null;
            try
            {
                transaction = currentSession.BeginTransaction();

                currentSession.Update(competition);

                transaction.Commit();
            }
            catch (HibernateException ex)
            {
                if (transaction != null)
                    transaction.Rollback();
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }
        }
    }
}
